<?php
class km_supplier_model extends MY_Model {
	var $model = 'km_supplier';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	supplier_id, supplier_kode, supplier_nama, supplier_alamat, supplier_telp, supplier_fax, supplier_email, supplier_website, supplier_npwp, supplier_npwp_pengukuhan, supplier_siup, supplier_situ, supplier_ijin_depkes, supplier_ijin_date, supplier_pkp, supplier_nokontrak, supplier_kontrak_awal, supplier_kontrak_akhir, supplier_konsinyasi, supplier_aktif, supplier_jatuh_tempo
					FROM	km_supplier 
					WHERE	(UPPER(supplier_kode) LIKE '%".strtoupper(post('keyword'))."%' OR 
							UPPER(supplier_nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'supplier_nama' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('km_supplier', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('km_supplier', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('km_supplier', array('supplier_kode' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('km_supplier', array('supplier_kode' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM km_supplier a join km_barang b on a.supplier_id=b.supp_id WHERE a.supplier_kode = '".$arr_rule['supplier_kode']."'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['supplier_kode']."]<br />";
			}else{
				$this->db->delete('km_supplier', array('supplier_kode' => $arr_rule['supplier_kode']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>